#!/usr/bin/env bash
#
# This script is for quickly generate helpful info
#
# It should be as independent as possible and should not source other files unless it has to
#
# TODO: How to get the Qt version which Quickshell was built against?

STY_RED='\e[31m'
STY_RST='\e[00m'

cd "$(dirname "$0")";export base="$(pwd)"
output_file=diagnose.result;rm $output_file
export LANG=C;export LC_ALL=C
case $(whoami) in
  root)echo -e "${STY_RED}[$0]: This script is NOT to be executed with sudo or as root. Aborting...${STY_RST}";exit 1;;
esac


x() { _exec "$@" 2>&1 | tee -a $output_file ; }
e() { _box "$@" | tee -a $output_file ; }
_box() {
  length=$(echo "$1" | wc -L);total_width=$((length + 2))
  #line=$(printf "═%.0s" $(seq 1 $total_width))
  #border_up="╔${line}╗";border_down="╚${line}╝"
  #border_vertical="║"
  line=$(printf "=%.0s" $(seq 1 $total_width))
  border_up="/${line}\\";border_down="\\${line}/"
  border_vertical="|"
  echo -e "\n$border_up"
  echo "$border_vertical $1 $border_vertical"
  echo "$border_down"
}
_exec() {
  printf "\n[===diagnose===] $*\n"
  "$@"
  err=$?;if [ ! $err -eq 0 ];then echo "[---EXIT $err---]";else echo "[---SUCCESS---]";fi
}
_check_distro_id() {
  OS_RELEASE_FILE=/etc/os-release
  if [[ -f "$OS_RELEASE_FILE" ]]; then
    OS_DISTRO_ID=$(awk -F'=' '/^ID=/ { gsub("\"","",$2); print tolower($2) }' ${OS_RELEASE_FILE} 2> /dev/null)
    OS_DISTRO_ID_LIKE=$(awk -F'=' '/^ID_LIKE=/ { gsub("\"","",$2); print tolower($2) }' ${OS_RELEASE_FILE} 2> /dev/null)
    echo "distro ID: $OS_DISTRO_ID"
    echo "distro ID_LIKE: $OS_DISTRO_ID_LIKE"
  else
    echo "$OS_RELEASE_FILE does not exist."
  fi
}
_check_distro() {
  lsb_release -a || cat /etc/os-release || cat /etc/lsb-release
}
_check_venv() {
  source $(eval echo $ILLOGICAL_IMPULSE_VIRTUAL_ENV)/bin/activate
  which python
  deactivate
}
_check_quickshell_version() {
  pacman -Q | grep -E 'quickshell|qt6-base'
}
_check_PKGBUILD_version() {
  pacman -Q | grep '^illogical-impulse-'
}

e "Checking git repo info"
x git remote get-url origin
x git rev-parse HEAD
x git status
x git submodule status --recursive

e "Checking distro"
x _check_distro_id
x cat os-release
#x _check_distro 

e "Checking variables"
x declare -p XDG_CACHE_HOME # ~/.cache
x declare -p XDG_CONFIG_HOME # ~/.config
x declare -p XDG_DATA_HOME # ~/.local/share
x declare -p XDG_STATE_HOME # ~/.local/state
x declare -p ILLOGICAL_IMPULSE_VIRTUAL_ENV # $XDG_STATE_HOME/quickshell/.venv

e "Checking directories/files"
x ls -l ~/.local/state/quickshell/.venv

#e "Checking command existence"
#commands=(yay pacman zypper apt dnf yum)
#commands+=(ags agsv1)
#commands+=(Hyprland hypr{ctl,idle,lock,picker})
#commands+=(uv)
#for i in "${commands[@]}";do x command -v $i;done

e "Checking versions"
x Hyprland --version
x _check_quickshell_version
x _check_PKGBUILD_version

e "Finished. Output saved as \"$output_file\"."
if ! command -v curl 2>&1 >>/dev/null ;then echo "\"curl\" not found, pastebin upload unavailable.";exit;fi
echo "(Optional) Do you agree to upload the file \"$output_file\" to the online pastebin (https://0x0.st)?"
echo "Notes:"
echo "1. It is a public service and the logfile will be expired in 15 days."
echo "2. You should have a look at the content of \"$output_file\" before agreeing to upload it."
echo "3. Only agree when necessary, typically when you are creating an issue and not able to upload the \"diagnose.result\" file there or copy-paste the output directly."
read -p "y=yes, n=no (default) ====> " p
case $p in
  [yY]) echo "OK, uploading..."
        curl -F'file=@diagnose.result' -Fexpires=360 https://0x0.st && \
          echo "Uploaded. Please attach the URL above when asking for help."
        ;;
  *) echo "Uploading aborted.";;
esac
